/**
 * Created by Administrator on 2017/6/14.
 */
(function ($) {
    $.extend({
        sum: function (array) {
            var total = 0;
            $.each(array, function (index, item) {
                item = $.trim(item);
                item = parseFloat(item) || 0;
                total += item;
            });
            return total;
        },

        average: function (array) {
            if ($.isArray(array)) {
                return $.sum(array) / array.length;
            }
            return 0;
        }
    });
})(jQuery);

(function ($) {
    $.fn.swapClass = function (class1, class2) {
        return this.each(function () {
            var $element = $(this);
            if ($element.hasClass(class1)) {
                $element.removeClass(class1).addClass(class2);
            } else if ($element.hasClass(class2)) {
                $element.removeClass(class2).addClass(class1);
            }
        });
    }
})(jQuery);

(function ($) {
    $.fn.shadow = function (options) {
        var opts = $.extend({}, $.fn.shadow.defaults, options);
        return this.each(function () {
            var $originalElement = $(this);
            for (var i = 0; i < opts.copies; i++) {
                var offset = opts.copyOffset(i);
                $originalElement
                    .clone()
                    .css({
                        position: 'absolute',
                        left: $originalElement.offset().left + offset.x,
                        top: $originalElement.offset().top + offset.y,
                        margin: 0,
                        zIndex: -1,
                        opacity: opts.opacity
                    })
                    .appendTo('body');
            }
        });
    };

    $.fn.shadow.defaults = {
        copies: 5,
        opacity: .1,
        copyOffset: function (index) {
            return {x: index, y: index};
        }
    };
})(jQuery);

(function ($) {
    $.widget('learning.tooltip', {
        _create: function () {
            if (!this.options.disabled) {
                this._tooltip = $('<div></div>')
                    .addClass('learning-tooltip-text ui-widget ui-state-hightlight ui-corner-all')
                    .hide()
                    .appendTo('body');

                this.element
                    .addClass('learning-tooltip-trigger')
                    .on('mouseenter.learning.tooltip', $.proxy(this._open, this))
                    .on('mouseleave.learning.tooltip', $.proxy(this._close, this));
            }
        },

        _open: function () {
            var elementOffset = this.element.offset();
            this._tooltip.css({
                position: 'absolute',
                left: elementOffset.left + this.options.offsetX,
                top: elementOffset.top + this.element.height() + this.options.offsetY
            }).text(this.options.content.call(this.element[0]));
            this._tooltip.show();
            this._trigger('open');
        },

        _close: function () {
            this._tooltip.hide();
            this._trigger('close');
        },

        destory: function () {
            this._tooltip.remove();
            this.element
                .removeClass('learning-tooltip-trigger')
                .off('.learning.tooltip');
            $.Widget.prototype.destory.apply(this, arguments);
        },

        options: {
            offsetX: 10,
            offsetY: 10,
            content: function () {
                return $(this).data('tooltip-text');
            }
        },

        open: function () {
            this._open();
        },

        close: function () {
            this._close();
        }
    });
})(jQuery);

$(document).ready(function () {
    var $inventory = $('#inventory tbody');
    var quantities = $inventory.find('td:nth-child(2)').map(function (index, item) {
        return $(item).text();
    }).get();
    var sum = $.sum(quantities);
    $('#sum').find('td:nth-child(2)').text(sum);
});

$(document).ready(function () {
    var $inventory = $('#inventory tbody');
    var prices = $inventory.find('td:nth-child(3)')
        .map(function (index, item) {
            return $(item).text();
        })
        .get();
    var average = $.average(prices);
    $('#average').find('td:nth-child(3)').text(average.toFixed(2));
});

$(document).ready(function () {
    $('table').click(function () {
        $('tr').swapClass('one', 'two');
    });
});

$(document).ready(function () {
    $.fn.shadow.defaults.copies = 10;
    $('h1').shadow({
        copyOffset: function (index) {
            return {x: -index, y: index};
        }
    });
});

$(document).ready(function () {
    $('a').tooltip({offsetX: -10, offsetY: 25});
    $('a').tooltip('option', 'offsetX', 20);
    $('a').tooltip('close');
});

$(document).ready(function () {
    $(this).on('tooltipopen.learning', function () {
        console.log('tooltip opened...');
    }).on('tooltipclose.learning', function () {
        console.log('tooltip closed...');
    });
});